package com.example.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.store.common.BaseContext;
import com.example.store.common.IpUtil;
import com.example.store.common.Result;
import com.example.store.entity.Cart;
import com.example.store.entity.Goods;
import com.example.store.entity.Time;
import com.example.store.service.CartService;
import com.example.store.service.GoodsService;
import com.example.store.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {//管理购物车相关

    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private TimeService timeService;

//    操作记录
    private void makeTime(String id, HttpServletRequest request, String operate){
        Time time = Time.createTime(id, 4);
        String ip = IpUtil.getIp(request);
        time.setIp(ip);
        time.setOperate(operate);
        timeService.save(time);
    }

//    添加购物车
    @PostMapping("/add")
    public Result<String> addCart(@RequestBody Cart cart, HttpServletRequest request){

//        用户校验
        String userId = BaseContext.getCurrentId();
        if(userId==null||(userId.charAt(0)!='u')){
            return Result.error("请登录");
        }
        if(cart.getGoodsid()==null){
            return Result.error("无商品");
        }
        Goods goods = goodsService.getById(cart.getGoodsid());
        if(goods==null){
            return Result.error("无商品");
        }

//        查询用户是否已经存在该类商品在购物车中，是则修改购物车的数量，否则新增购物车
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserid, userId);
        queryWrapper.eq(Cart::getGoodsid, cart.getGoodsid());
        Cart cart1 = cartService.getOne(queryWrapper);
        if(cart1==null){
            cart1 = new Cart();
            cart1.setUserid(userId);
            cart1.setGoodsid(cart.getGoodsid());
            cart1.setName(goods.getName());
            cart1.setPrice(goods.getPrice());
            cart1.setCount(cart.getCount());
            cartService.save(cart1);
        }
        else{
            cart1.setCount(cart1.getCount()+ cart.getCount());
            cartService.updateById(cart1);
        }

//        记录日志
        makeTime(userId, request, "添加购物车:"+cart1.getId());

        return Result.success("已添加");
    }

//    分页
    @PostMapping("/page")
    public Result<Page<Cart>> page(@RequestBody Map<String, Integer> pageNum, HttpServletRequest request){

//        用户、参数校验
        String userId = BaseContext.getCurrentId();
        if(userId==null||(userId.charAt(0)!='u')){
            return Result.error("请登录");
        }

        if(pageNum.get("pageNum")==null){
            return Result.error("无效页码");
        }

//        分页操作
        Page<Cart> pageInfo = new Page<>(pageNum.get("pageNum"), 10);
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserid, userId);
        cartService.page(pageInfo, queryWrapper);

//        记录日志
        makeTime(userId, request, "购物车分页");

        return Result.success(pageInfo);
    }


//    由购物车跳转到订单
    @PostMapping("/toOrder")
    public Result<Cart> toOrder(@RequestBody Cart cart){
//        用户校验
        String userId = BaseContext.getCurrentId();
        if(userId==null||(userId.charAt(0)!='u')){
            return Result.error("请登录");
        }
        Cart cart1 = cartService.getById(cart.getId());
        if(!userId.equals(cart1.getUserid())){
            return Result.error("不是你的订单");
        }

//        返回购物车的内容，用于生成订单
        return Result.success(cart1);
    }

//    移除购物车
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Cart cart, HttpServletRequest request){
//        用户校验
        String userId = BaseContext.getCurrentId();
        if(userId==null||(userId.charAt(0)!='u')){
            return Result.error("请登录");
        }

        cartService.removeById(cart.getId());

//        记录日志
        makeTime(userId, request, "移除购物车:"+cart.getId());
        return Result.success("已删除");
    }

//    用于管理员查询
    @PostMapping("/search")
    public Result<Cart> search(@RequestBody Cart cart){
        //        用户校验
        String userId = BaseContext.getCurrentId();
        if(userId==null||!userId.equals("root")){
            return Result.error("请管理员登录");
        }

        Cart cart1 = cartService.getById(cart.getId());
        if(cart1==null){
            return Result.error("无");
        }
        return Result.success(cart1);
    }
}
